package List.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/5
 */
/* 按照斗地主的规则，完成**洗牌发牌**的动作。
        * 具体规则：
        * 使用54张牌打乱顺序,三个玩家参与游戏，三人交替摸牌，每人17张牌，最后三张留作底牌。
        * 牌进行排序、拿大王的可以拿底牌。
        *
        * 1.做牌  Card 花色color、数字num、索引index  --》 存储至List<Card>
 * 2.乱序  --> Collections的shuffle()
 * 3.发牌  --> 51张牌 %3  + 剩余3张牌作为底牌  --》 存储至List<Card>
 * 4.底牌  --> 拿取大王的可以拿底牌
 * 5.排序  --> 将各玩家的牌进行排序
 *
 * "♠", "♥", "♣", "♦"
 * "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"
 */
public class Doudizhu {

    public static void main(String[] args) {
        List<Card> list = new ArrayList();
        String[] color={"♠", "♥", "♣", "♦"};
        String[] num={"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        int index=2;
        for(int i=0;i<num.length;i++){
            for(int j=0;j<color.length;j++){
                list.add(new Card(color[j],num[i],index));
            }
            index++;
        }
        Card dawang=new Card("大","王",0);
        Card xiaowang=new Card("小","王",1);
        list.add(dawang);
        list.add(xiaowang);
        List<Card> player1=new ArrayList<>();
        List<Card> player2=new ArrayList<>();
        List<Card> player3=new ArrayList<>();
        List<Card> dipai=new ArrayList<>();
        while (true) {
            dipai.clear();
            player1.clear();
            player2.clear();
            player3.clear();
            Collections.shuffle(list);
            for (int i = 0; i <51; i++) {
                if(i%3==0){
                    player1.add(list.get(i));
                }
                if(i%3==1){
                    player2.add(list.get(i));
                }
                if(i%3==2){
                    player3.add(list.get(i));
                }
            }
            dipai.addAll(list.subList(51,list.size()));
            //如果大王在底牌中重新发牌
            if(ishas(dipai)){
                continue;
            }
            if(ishas(player1)){
                player1.addAll(dipai);
                break;
            }
            if(ishas(player2)){
                player2.addAll(dipai);
                break;
            }
            if(ishas(player3)){
                player3.addAll(dipai);
                break;
            }

        }
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        System.out.println("player1:"+player1);
        System.out.println("player2:"+player2);
        System.out.println("player3:"+player3);
        System.out.println("dipai  :"+ dipai);

    }
    public static boolean ishas(List<Card> cards){
        for(Card c:cards){
            if(c.equals(new Card("大","王",0))){
                return true;
            }
        }
        return false;
    }
}
